package com.shengde.jinwawaforleader.model.myJob.monitoring;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.hyphenate.easeui.widget.EaseSwitchButton;
import com.shengde.jinwawaforleader.R;
import com.shengde.jinwawaforleader.base.BaseActivity;
import com.shengde.jinwawaforleader.base.CommonBaseAdapter;
import com.shengde.jinwawaforleader.base.ViewHolder;
import com.shengde.jinwawaforleader.bean.PermissionInfo;
import com.shengde.jinwawaforleader.bean.User;
import com.shengde.jinwawaforleader.network.BaseProtocol;
import com.shengde.jinwawaforleader.network.protocolbean.ProTeacherPerDetiailMonitoringList;
import com.shengde.jinwawaforleader.network.protocolbean.ProVideoShowClickGood;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

/**
 *各个班级 授权人详情
 * Created by Administrator on 2016/10/24 0024.
 */
public class ParentPermissionDetiailActivity extends BaseActivity {
	private static final int FINSH = 1;
	private static final String TAG = "ParntPermissailActivity";
	private TextView tv_back;
	private TextView tv_title;
	private TextView fill_add;
	private TextView tv_name_tag; //所有学生
	private String deviceSerial;
	private String className;
	private int classid;
	private ListView lv_teacher_permission;
	private CommonBaseAdapter teacherPermissonAdapter;
	private EaseSwitchButton ease_switch_btn_all;//所有权限开启按钮
	List<ProTeacherPerDetiailMonitoringList.SlistBean> SlistBean;
	private ArrayList<PermissionInfo> Ids = new ArrayList<>();

	private Handler mHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			switch (msg.what) {
				case FINSH:
					if (teacherPermissonAdapter == null) {
						teacherPermissonAdapter = new CommonBaseAdapter(ParentPermissionDetiailActivity.this, SlistBean, R.layout.teacher_permisson_activity_item) {
							@Override
							public void convert(final ViewHolder holder, final int position) {
								final EaseSwitchButton ease_switch_btn = holder.getView(R.id.ease_switch_btn);
								holder.setText(R.id.tv_teacher_name,SlistBean.get(position).getName());

								if (SlistBean.get(position).getState()!=1){
									ease_switch_btn.closeSwitch();
								}else {
									ease_switch_btn.openSwitch();
								}
								ease_switch_btn.setOnClickListener(new View.OnClickListener() {
									@Override
									public void onClick(View v) {
										if (ease_switch_btn.isSwitchOpen()) {
											ease_switch_btn.closeSwitch();
											PermissionInfo tfiac = new PermissionInfo();
											tfiac.userid = SlistBean.get(position).getStudentid();
											tfiac.check = "2";
											Ids.add(tfiac);
										} else {
											ease_switch_btn.openSwitch();
											PermissionInfo tfiac = new PermissionInfo();
											tfiac.userid = SlistBean.get(position).getStudentid();
											tfiac.check = "1";
											Ids.add(tfiac);
										}
									}
								});
							}
						};

						lv_teacher_permission.setAdapter(teacherPermissonAdapter);
					} else {
						teacherPermissonAdapter.notifyDataSetChanged();
					}
					break;
			}
		}
	};

	@Override
	public void onRoot(Bundle savedInstanceState) {
		setContentView(R.layout.teacher_permisson_activity);
	}

	@Override
	public void initView() {
		tv_back = (TextView) findViewById(R.id.tv_back);
		tv_title = (TextView) findViewById(R.id.tv_title);
		fill_add = (TextView) findViewById(R.id.fill_add);
		tv_name_tag = (TextView) findViewById(R.id.tv_name_tag);
		lv_teacher_permission = (ListView) findViewById(R.id.lv_teacher_permission);
		ease_switch_btn_all = (EaseSwitchButton) findViewById(R.id.ease_switch_btn_all);
	}

	@Override
	public void initData() {
		className = getIntent().getStringExtra("className");
		tv_title.setText(className+ getString(R.string.permission_setting));
		fill_add.setText(getString(R.string.save));
		tv_name_tag.setText(getString(R.string.all_student));
		deviceSerial = getIntent().getStringExtra("deviceSerial");
		classid = getIntent().getIntExtra("classid",0);
		Log.i(TAG, "initData: classid" + classid);

		showStudentPermissionList();
	}


	@Override
	public void initListener() {
		tv_back.setOnClickListener(this);
		fill_add.setOnClickListener(this);
		ease_switch_btn_all.setOnClickListener(this);
	}

	@Override
	public void onWidgetClick(View view) {
		switch (view.getId()) {
			case R.id.tv_back:
				finish();
				break;
			case R.id.fill_add:
				showOpenPermission();
				finish();
				break;
			case R.id.ease_switch_btn_all:  //开启所有教师权限按钮
				if (ease_switch_btn_all.isSwitchOpen()) {
					ease_switch_btn_all.closeSwitch();
				} else {
					ease_switch_btn_all.openSwitch();
				}
				break;
		}
	}

	/**
	 * 点击 保存 确认开启权限
	 */
	private void showOpenPermission() {
		String url = "";
		if (User.isTest) {
			url = BaseProtocol.HOST_BASE_TEST + "/camera/checkLookStudent";
		} else {
			url = BaseProtocol.HOST_BASE + "/camera/checkLookStudent";
		}
		//"http://192.168.1.40:8080/api/camera/checkLookTeacher"
		RequestParams params  = BaseProtocol.getRequestParams(url);
		params.addParameter("deviceSerial",deviceSerial);
		params.addBodyParameter("ids",new Gson().toJson(Ids));
		x.http().post(params, new Callback.CommonCallback<String>() {
			@Override
			public void onSuccess(String result) {
				ProVideoShowClickGood pro_monitoring_list = new Gson().fromJson(result, ProVideoShowClickGood.class);
				Log.i(TAG, "onSuccess: code" + pro_monitoring_list.getCode());
				if (pro_monitoring_list != null && pro_monitoring_list.getCode() == 0) {
					Toast.makeText(ParentPermissionDetiailActivity.this, getString(R.string.permissionSuccess), Toast.LENGTH_SHORT).show();
				}
			}

			@Override
			public void onError(Throwable ex, boolean isOnCallback) {
				Log.i(TAG, "onError: ex" + ex);
			}

			@Override
			public void onCancelled(CancelledException cex) {

			}

			@Override
			public void onFinished() {

			}
		});

	}

	/**
	 * 网络请求 获取授权的学生列表(以班级为单位)
	 */
	private void showStudentPermissionList() {
		String url = "";
		if (User.isTest) {
			url = BaseProtocol.HOST_BASE_TEST + "/camera/cameraLookstudent";
		} else {
			url = BaseProtocol.HOST_BASE + "/camera/cameraLookstudent";
		}
		Log.i(TAG, "showStudentPermissionList: deviceSerial"+deviceSerial);
		Log.i(TAG, "showStudentPermissionList: classid"+classid);
		//http://192.168.1.40:8080/api/camera/checkLookTeacher
		RequestParams params  = BaseProtocol.getRequestParams(url);
		params.addParameter("deviceSerial",deviceSerial);
		params.addParameter("classid",classid);
		x.http().post(params, new Callback.CommonCallback<String>() {
			@Override
			public void onSuccess(String result) {
				ProTeacherPerDetiailMonitoringList pro_monitoring_list = new Gson().fromJson(result, ProTeacherPerDetiailMonitoringList.class);
				Log.i(TAG, "onSuccess: code" + pro_monitoring_list.getCode());
				Log.i(TAG, "onSuccess: beanSize" +pro_monitoring_list.getSlist().size());
				Log.i(TAG, "onSuccess: pro_monitoring_list" +pro_monitoring_list.toString());
				if (pro_monitoring_list != null && pro_monitoring_list.getCode() == 0) {

					SlistBean = pro_monitoring_list.getSlist();
					mHandler.sendEmptyMessage(FINSH);
				}
			}

			@Override
			public void onError(Throwable ex, boolean isOnCallback) {
				Log.i(TAG, "onError: ex" + ex);
			}

			@Override
			public void onCancelled(CancelledException cex) {

			}

			@Override
			public void onFinished() {

			}
		});
	}
}
